package com.vincce.sport.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.util.EntityUtils;

/**
 * Created by henry on 3/12/14.
 */
public class SimpleStorageClient {

	public static String REQUEST_URI_PREFIX = "http://121.41.47.15:8081";
	public static String REQUEST_URI2_PREFIX = "http://121.41.47.15:8081/images";
	private static String REQUEST_UPLOAD = "/upload";
	private static String REQUEST_ARCHIVE = "/archive";
	private static String PARAMETER_FILE = "file";
	private static String PARAMETER_CATEGORY_ID = "categoryId";
	private static String PARAMETER_PATH = "path";

	// 图片访问地址：http://112.124.25.199/storage 加文件路径
	
	
	@SuppressWarnings("static-access")
	public void setImageUrl(String imageUrl) {
		this.REQUEST_URI_PREFIX = imageUrl;
	}
	
	@SuppressWarnings("static-access")
	public void setImageUrl2(String imageUrl2) {
		this.REQUEST_URI2_PREFIX = imageUrl2;
	}
	 

	/**
	 * 返回結果
	 */
	public static class StorageResult {
		/**
		 * 狀態碼，與http狀態碼相同，200正常
		 */
		public int statusCode;
		/**
		 * 返回信息，可能是上傳時生成的文件模式，或者是報錯信息
		 */
		public String info;

		public StorageResult(int statusCode, String info) {
			this.statusCode = statusCode;
			this.info = info;
		}

		@Override
		public String toString() {
			return "statusCode : " + statusCode + "\ninfo : " + info;
		}
	}

	private static ResponseHandler<StorageResult> responseHandler = new ResponseHandler<StorageResult>() {
		@Override
		public StorageResult handleResponse(final HttpResponse response) throws IOException {
			return new StorageResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity()));
		}
	};

	/**
	 * 上傳文件
	 * @param categoryId 分類id
	 * @param is 文件流
	 * @return 上傳結果
	 * 	statusCode: 200正常，其他非正常
	 * 	info: 生成的文件模式，或報錯信息
	 * @throws java.io.IOException
	 */
	public static StorageResult upload(int categoryId, InputStream is, String fileType) throws IOException {
		return Request
				.Post(REQUEST_URI_PREFIX + REQUEST_UPLOAD)
				.body(MultipartEntityBuilder
						.create()
						.addBinaryBody(PARAMETER_FILE, is, ContentType.create("image/jpg"), System.currentTimeMillis() + fileType)
						.addTextBody(PARAMETER_CATEGORY_ID, categoryId + "")
						.build())
				.execute()
				.handleResponse(responseHandler);
	}

	/**
	 * 歸檔文件
	 * @param categoryId 分類id
	 * @param path 文件模式
	 * @return 歸檔結果
	 * 	statusCode: 200正常，其他非正常
	 * 	info: 報錯信息
	 * @throws java.io.IOException
	 */
	public static StorageResult archive(int categoryId, String path) throws IOException {
		return Request
				.Post(REQUEST_URI_PREFIX + REQUEST_ARCHIVE)
				.bodyForm(Form.form()
						.add(PARAMETER_CATEGORY_ID, categoryId + "")
						.add(PARAMETER_PATH, path)
						.build(), Charset.forName("UTF-8"))
				.execute()
				.handleResponse(responseHandler);
	}

	/**
	 * 上傳文件
	 * @param categoryId 分類id
	 * @param file 文件對象
	 * @return 上傳結果
	 * 	statusCode: 200正常，其他非正常
	 * 	info: 生成的文件模式，或報錯信息
	 * @throws java.io.IOException
	 */
	public static StorageResult upload(int categoryId, File file) throws IOException {
		FileInputStream fis = new FileInputStream(file);
		String fileName = file.getName();
    	String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
		StorageResult result = upload(categoryId, fis, fileType);
		
		fis.close();
		return result;
	}



	public static void main(String[] args) throws IOException {
		System.out.println(upload(2, new File("/Users/henry/IMG_0211.jpg")));
//		System.out.println(archive(1, "/so/bg/iw-%s.jpg"));
	}

}
